package org.example.netty.handler;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import org.example.netty.protocol.MsgProtos;
import org.example.utils.LogUtils;

/**
 * protobuf业务处理器
 */
public class ProtobufBusinessHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        MsgProtos.Msg msg1 = (MsgProtos.Msg)msg;
        LogUtils.info("id : " + msg1.getId());
        LogUtils.info("content : " + msg1.getContent());
        LogUtils.info("current thread : " + Thread.currentThread().getName());
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // 读取完毕之后发送数据
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello，服务端", CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        LogUtils.info("客户端连接断开..." + cause.getMessage());
    }
}
